package Offer.offer2020;

/**
 * 打印从1到最大的n位数
 * 是否需要考虑大数问题
 */
public class Test12 {
    public static void main(String[] args) {
        print1ToN(2);
    }

    /**
     * 考虑大数问题
     * 最大的3位数  模拟加法
     * @param n
     */
    public static void print1ToN(int n) {
        int[] array = new int[n];
        for (int i = 0; i < n; i++) {
            array[i] = 0;
        }

        while (addOne(array)) {
            print(array);
        }
    }

    private static boolean addOne(int[] array) {
        int takeOver = 0;
        for (int i = array.length - 1; i >= 0; i--) {
            int sum = array[i] + takeOver;
            if (i == array.length - 1) {
                sum++;
            }
            if (sum >= 10) {
                if (i == 0) {
                    return false;
                }
                takeOver = 1;
                sum -= sum;
                array[i] = sum;
            } else {
                array[i] = sum;
                break;
            }

        }
        return true;
    }

    private static void print(int[] array) {
        boolean flag = false;
        for (int i = 0; i < array.length; i++) {
            if (array[i] != 0) {
                flag = true;
            }
            if (flag) {
                System.out.print(array[i]);
            }
        }
        System.out.print(" ");
    }




    /**
     * 不考虑大数问题
     * 最大的3位数
     * @param n
     */
    public static void print1ToN2(int n) {
        long maxNum = 1;
        for (int i = 1; i <= n; i++) {
            maxNum = 10 * maxNum;
        }

        for (int i = 1; i < maxNum; i++) {
            System.out.print(i);
            System.out.print(" ");
        }
    }
}
